package test;

import cn.hutool.core.date.DateUtil;
import com.hx.entry.ExcelFieldData;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Date;

public class ReflectionExample {
    public static void main(String[] args) {
        ExcelFieldData excelFieldData = new ExcelFieldData();
        excelFieldData.setBaseCode("11111");
        setV(excelFieldData,"rowNum","123","");
        System.out.println(excelFieldData.getRowNum());
    }
    @SuppressWarnings("unchecked")
    public static <T> void setV(T entity,String setName,String setValue,String format) {
        Class<T> clazz = (Class<T>) entity.getClass();
        //获取所有的bean中所有的成员变量
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (int j = 0; j < fields.length; j++) { // 遍历所有属性
                String name = fields[j].getName(); // 获取属性的名字
                if(name.equals(setName)){

                    name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写，方便构造get，set方法
                    String type = fields[j].getGenericType().toString(); // 获取属性的类型
                    if (type.equals("class java.lang.String")) { // 如果type是类类型，则前面包含"class "，后面跟类名
                        Method m = entity.getClass().getMethod("set"+name,String.class);
                        m.invoke(entity, setValue);
                    }
                    if (type.equals("class java.util.Date")) {
                        Method	m = entity.getClass().getMethod("set"+name,Date.class);
                        Date date = DateUtil.parse(setValue,format);
                        m.invoke(entity, date);
                    }
                    if (type.equals("class java.lang.Integer")) {
                        Method m = entity.getClass().getMethod("set"+name,Integer.class);
                        m.invoke(entity, Integer.valueOf(setValue));
                    }
                    if (type.equals("class java.lang.Double")) {
                        Method	m = entity.getClass().getMethod("set"+name,Double.class);
                        m.invoke(entity, Double.valueOf(setValue));
                    }
                    if (type.equals("class java.lang.Long")) {
                        Method	m = entity.getClass().getMethod("set"+name,Long.class);
                        m.invoke(entity, Long.valueOf(setValue));
                    }
                    if (type.equals("class java.lang.Boolean")) {
                        Method	m = entity.getClass().getMethod("set"+name,Boolean.class);
                        m.invoke(entity, Boolean.valueOf(setValue));
                    }
                }
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    public <T> void scoreSystem(T t, Class<T> cls) {
        // 给传入对象赋值名称
        try {
            Class<? super T> superclass = cls.getSuperclass();
            Field nameField = superclass.getDeclaredField("name");
            Type genericType = nameField.getGenericType();
            String typeName = genericType.getTypeName();
            assert "java.lang.String".equals(typeName);
            nameField.setAccessible(true);
            String studentName = nameField.get(t).toString();
            int schoolReport = 0;
            switch (studentName) {
                case "张三":
                    schoolReport = 100;
                    break;
                case "李四":
                    schoolReport = 114;
                    break;
                case "王五":
                    schoolReport = 127;
                    break;
            }
            Field subjectField = superclass.getDeclaredField("subject");
            subjectField.setAccessible(true);
            subjectField.set(t, "语文");
            Field scoreField = superclass.getDeclaredField("score");
            scoreField.setAccessible(true);
            scoreField.set(t, schoolReport);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}